#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
use std::mem;
use crate::alloc_testing::*;
extern "C" {
        }
pub type size_t = libc::c_ulong;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _Queue {
    pub head: *mut QueueEntry,
    pub tail: *mut QueueEntry,
}
pub type QueueEntry = _QueueEntry;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _QueueEntry {
    pub data: QueueValue,
    pub prev: *mut QueueEntry,
    pub next: *mut QueueEntry,
}
pub type QueueValue = *mut libc::c_void;
pub type Queue = _Queue;
#[no_mangle]
pub extern "C" fn queue_new() -> *mut Queue {
    let mut queue: *mut Queue = std::ptr::null_mut();

    unsafe {
        queue = alloc_test_malloc(std::mem::size_of::<Queue>() as libc::c_ulong) as *mut Queue;
    }

    if queue.is_null() {
        return std::ptr::null_mut();
    }

    let mut boxed_queue = unsafe { Box::from_raw(queue) };
    boxed_queue.head = std::ptr::null_mut();
    boxed_queue.tail = std::ptr::null_mut();
    let queue = Box::into_raw(boxed_queue);

    queue
}
#[no_mangle]
pub extern "C" fn queue_is_empty(mut queue: *mut Queue) -> libc::c_int {
    let boxed_queue = unsafe { Box::from_raw(queue) };
    let result = (boxed_queue.head.is_null()) as libc::c_int;
    let queue = Box::into_raw(boxed_queue);
    result
}
#[no_mangle]
pub extern "C" fn queue_pop_head(mut queue: *mut Queue) -> QueueValue {
    let mut entry: *mut QueueEntry = std::ptr::null_mut();
    let mut result: QueueValue = std::ptr::null_mut();

    if queue_is_empty(queue) != 0 {
        return std::ptr::null_mut();
    }

    let mut boxed_queue = unsafe { Box::from_raw(queue) };
    entry = boxed_queue.head;
    boxed_queue.head = unsafe { (*entry).next };
    result = unsafe { (*entry).data };

    if boxed_queue.head.is_null() {
        boxed_queue.tail = std::ptr::null_mut();
    } else {
        unsafe { (*boxed_queue.head).prev = std::ptr::null_mut(); }
    }

    unsafe { alloc_test_free(entry as *mut libc::c_void); }
    queue = Box::into_raw(boxed_queue);

    result
}
#[no_mangle]
pub extern "C" fn queue_free(mut queue: *mut Queue) {
    if !queue.is_null() {
        let boxed_queue = unsafe { Box::from_raw(queue) };
        while queue_is_empty(queue) == 0 {
            queue_pop_head(queue);
        }
        alloc_test_free(queue as *mut libc::c_void);
        let queue = Box::into_raw(boxed_queue);
    }
}
#[no_mangle]
pub extern "C" fn queue_push_head(
    mut queue: *mut Queue,
    mut data: QueueValue,
) -> libc::c_int {
    let mut new_entry: *mut QueueEntry = std::ptr::null_mut();

    new_entry = unsafe {
        alloc_test_malloc(std::mem::size_of::<QueueEntry>() as libc::c_ulong)
            as *mut QueueEntry
    };

    if new_entry.is_null() {
        return 0;
    }

    let mut boxed_new_entry = unsafe { Box::from_raw(new_entry) };
    boxed_new_entry.data = data;
    boxed_new_entry.prev = std::ptr::null_mut();
    boxed_new_entry.next = unsafe { (*queue).head };

    let mut boxed_queue = unsafe { Box::from_raw(queue) };
    if boxed_queue.head.is_null() {
        boxed_queue.head = new_entry;
        boxed_queue.tail = new_entry;
    } else {
        unsafe { (*boxed_queue.head).prev = new_entry };
        boxed_queue.head = new_entry;
    }
    queue = Box::into_raw(boxed_queue);
    new_entry = Box::into_raw(boxed_new_entry);

    return 1;
}
#[no_mangle]
pub extern "C" fn queue_peek_head(mut queue: *mut Queue) -> QueueValue {
    if queue_is_empty(queue) != 0 {
        return std::ptr::null_mut();
    } else {
        let boxed_queue = unsafe { Box::from_raw(queue) };
        let data = unsafe { (*boxed_queue.head).data };
        let queue = Box::into_raw(boxed_queue);
        return data;
    }
}
#[no_mangle]
pub extern "C" fn queue_push_tail(
    mut queue: *mut Queue,
    mut data: QueueValue,
) -> libc::c_int {
    let new_entry: *mut QueueEntry = unsafe {
        alloc_test_malloc(std::mem::size_of::<QueueEntry>() as libc::c_ulong) as *mut QueueEntry
    };

    if new_entry.is_null() {
        return 0;
    }

    let mut boxed_new_entry = unsafe { Box::from_raw(new_entry) };
    boxed_new_entry.data = data;
    boxed_new_entry.prev = unsafe { (*queue).tail };
    boxed_new_entry.next = std::ptr::null_mut();

    let mut boxed_queue = unsafe { Box::from_raw(queue) };
    if boxed_queue.tail.is_null() {
        boxed_queue.head = new_entry;
        boxed_queue.tail = new_entry;
    } else {
        unsafe { (*boxed_queue.tail).next = new_entry };
        boxed_queue.tail = new_entry;
    }
    queue = Box::into_raw(boxed_queue);
    Box::into_raw(boxed_new_entry);

    return 1;
}
#[no_mangle]
pub extern "C" fn queue_pop_tail(mut queue: *mut Queue) -> QueueValue {
    let mut entry: *mut QueueEntry = std::ptr::null_mut();
    let mut result: QueueValue = std::ptr::null_mut();

    if queue_is_empty(queue) != 0 {
        return std::ptr::null_mut();
    }

    let mut boxed_queue = unsafe { Box::from_raw(queue) };
    entry = boxed_queue.tail;
    boxed_queue.tail = unsafe { (*entry).prev };
    result = unsafe { (*entry).data };

    if boxed_queue.tail.is_null() {
        boxed_queue.head = std::ptr::null_mut();
    } else {
        unsafe { (*boxed_queue.tail).next = std::ptr::null_mut(); }
    }

    unsafe { alloc_test_free(entry as *mut libc::c_void); }
    queue = Box::into_raw(boxed_queue);

    result
}
#[no_mangle]
pub extern "C" fn queue_peek_tail(mut queue: *mut Queue) -> QueueValue {
    if queue_is_empty(queue) != 0 {
        return std::ptr::null_mut();
    } else {
        let boxed_queue = unsafe { Box::from_raw(queue) };
        let result = unsafe { (*boxed_queue.tail).data };
        let queue = Box::into_raw(boxed_queue);
        return result;
    }
}
